//
//  LeftViewController.m
//  MoGuRadio
//
//  Created by 李响 on 15/6/7.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "LeftViewController.h"
#import "TableViewZuoCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "Table.h"
<<<<<<< HEAD

=======
#import "AboutUsViewController.h"
#import "YRSideViewController.h"
#import "IntroduceViewController.h"

#import "BaseViewController.h"
>>>>>>> c1f2daaa683b676696d15cbdc25e38c446ffa905
@interface LeftViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *sourceArr;
@property (nonatomic, retain) UIButton *fxButton;

@property (nonatomic, retain) UILabel *comeLabel;
@property (nonatomic, retain) NSArray *BBQ;

@property (nonatomic, retain) UIButton *sinaButton;
@end

@implementation LeftViewController
- (void)dealloc
{
    [_BBQ release];
    [_tableView release];
    [_sourceArr release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.sourceArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 背景图片
    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1)];
    backgroundImage.image = [UIImage imageNamed:@"grayBackground.jpeg"];
    [self.view addSubview:backgroundImage];
    [backgroundImage release];
    
    
    [self setUpTableView];
    [self viewXianshi];
    
    self.BBQ = @[@{@"kkk" : @"注册/登陆"},
                 @{@"kkk" : @"蘑菇推荐"},
                 @{@"kkk" : @"帮我们出名"},
                 @{@"kkk" : @"关于我们"},
                 @{@"kkk" : @"清除缓存"},
                 @{@"kkk" : @"联系我们"},
                 @{@"kkk" : @"功能介绍"},
                 ];
    NSMutableArray * array2 = [NSMutableArray arrayWithArray:self.BBQ];
    self.sourceArr = [Table mainArrWithDicArr:array2];
    [self.tableView reloadData];
    
    self.comeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 5 / 12, self.view.bounds.size.height * 63.5 / 100, 200, 50)];
    [self.view addSubview:self.comeLabel];
    
    
    
}

- (void)setUpTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 9 / 20 / 2, self.view.bounds.size.width, self.view.bounds.size.height * 10 / 11 - self.view.bounds.size.height * 9 / 20 / 2) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    _tableView.alpha = 0.3;
    
    [self.tableView registerClass:[TableViewZuoCell class] forCellReuseIdentifier:@"diyi"];
    [_tableView release];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.comeLabel.text = [self sizeOfFiles];
    [self.comeLabel sizeToFit];
    self.comeLabel.font = [UIFont systemFontOfSize:14];
}
- (void)viewXianshi
{
    
    UIView *viewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 20 / 2)];
    viewOne.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0];
    [self.view addSubview:viewOne];
    
    
    UIImageView *imageOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 20 / 2)];
    imageOne.image = [UIImage imageNamed:@"top.png"];
    imageOne.alpha = 0.9;
    [viewOne addSubview:imageOne];
    [viewOne release];
    
    
    // 默认头像图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 14, self.view.bounds.size.height / 20, 100, 100)];
    imageView.image = [UIImage imageNamed:@"mogu180x180"];
    imageView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    imageView.layer.borderWidth = 3;
    imageView.layer.cornerRadius = 50;
    imageView.clipsToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];
    
    
    UIView *viewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 161 /90/2, self.view.bounds.size.width, self.view.bounds.size.height * 1 / 9)];
    viewTwo.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0];
    [self.view addSubview:viewTwo];
    
    UIImageView *imageTow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 20 / 2)];
    imageTow.image = [UIImage imageNamed:@"Down.png"];
    imageTow.alpha = 0.7;
    [viewTwo addSubview:imageTow];
    [viewTwo release];
}

// 显示清理缓存剩余几M
- (NSString *)sizeOfFiles{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *files = [fileManager subpathsAtPath:cachesDir];
    int theFileSize = 0;
    for (NSString *file in files) {
        NSDictionary *attributes = [fileManager attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", cachesDir, file] error:nil];
        theFileSize += [[attributes objectForKey:NSFileSize] intValue];
        NSLog(@"%d", theFileSize);
    }
    return [NSString stringWithFormat:@"%.2fM", theFileSize/1024.0/1024.0];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewZuoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"diyi"];
    
    Table *main = [self.sourceArr  objectAtIndex:indexPath.row];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.9    green:0.9 blue:0.9 alpha:1.0];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    
    cell.main = main;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.bounds.size.height * 21 / 110 / 2;
    return VIEW_HEIGHT / 6;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        YRSideViewController *yrsVC = (YRSideViewController *)self.view.window.rootViewController;
        UITabBarController *tabVC = (UITabBarController *)[yrsVC rootViewController];
        [yrsVC hideSideViewController:YES];
        tabVC.selectedViewController = [tabVC.viewControllers objectAtIndex:1];
    }
    if (indexPath.row == 0)
    {
        [self sinaBtnClick];
    }
    if (indexPath.row == 5) {
        YRSideViewController *yrsVC = (YRSideViewController *)self.view.window.rootViewController;
        UITabBarController *tabVC = (UITabBarController *)[yrsVC rootViewController];
        [yrsVC hideSideViewController:YES];
        UINavigationController *navVC = (UINavigationController *)tabVC.selectedViewController;
        [navVC pushViewController:[[AboutUsViewController alloc] init] animated:YES];
    }
    if (indexPath.row == 6) {
        YRSideViewController *yrsVC = (YRSideViewController *)self.view.window.rootViewController;
        UITabBarController *tabVC = (UITabBarController *)[yrsVC rootViewController];
        [yrsVC hideSideViewController:YES];
        UINavigationController *navVC = (UINavigationController *)tabVC.selectedViewController;
        [navVC pushViewController:[[IntroduceViewController alloc] init] animated:YES];
    }
/*
    if (indexPath.row == 4) {

    if (indexPath.row == 2) {
        

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
            for (NSString *p in files) {
                NSError *error;
                NSString *path = [cachPath stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
            [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
        });
    }
*/
    if (indexPath.row == 4) {
        [self clear];
    }
}
- (void)clear
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *files = [fileManager subpathsAtPath:cachesDir];
    for (NSString *file in files) {
        [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", cachesDir, file] error:nil];
    }
    self.comeLabel.text = [self sizeOfFiles];
    [self.comeLabel sizeToFit];
}
//-(void)clearCacheSuccess
//{
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"缓存清理成功^_^" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [self.view addSubview:alertView];
//    [alertView show];
//    [alertView release];
//    
//    NSLog(@"清理缓存成功");
//}

//- (void)sina
//{
//    self.sinaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.sinaButton.frame = CGRectMake(20, 100, 50, 50);
//    self.sinaButton.backgroundColor = [UIColor blackColor];
//    [self.sinaButton addTarget:self action:@selector(sinaBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:self.sinaButton];
//}

//微博登陆
- (void)sinaBtnClick
{
    NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeSina];
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        NSLog(@"response is %@",response);
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
            NSLog(@"=========%@",snsAccount.accessToken);
        }
    });
}

//回调方法

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response

{
    
    if (response.viewControllerType == UMSViewControllerOauth) {
        
        NSLog(@"didFinishOauthAndGetAccount response is %@",response);
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end